package com.oa.modules.approvalsec1.controller;

import com.oa.common.utils.*;
import com.oa.modules.approvalsec1.entity.CertificationEntity;
import com.oa.modules.approvalsec1.service.CertificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "实名认证")
@RequestMapping("nameCertification")
public class NameCertificationController {

    @Autowired
    private CertificationService certificationService;

    @PostMapping("/realName")
    @ApiOperation(value = "实名认证",httpMethod = "POST")
    @ApiImplicitParam(name = "files,name", value = "传参:files:文件数组,files[0]:身份证正面，files[1]:身份证反面,name:姓名", required = true)
    public R uploadFile(@RequestParam("files") MultipartFile[] files, CertificationEntity certificationEntity) throws IllegalStateException, IOException {
        List<Map<String,Object>> listMap = new ArrayList<>();
        List<String> urlList = new ArrayList<>();
        for (int i=0;i<files.length;i++){
            MultipartFile file = files[i];
            if (file != null) {
                String fileName = file.getOriginalFilename();// 文件原名称
                String accessToken = SenInfoCheckUtil.getAccessToken();
                if(SenInfoCheckUtil.imgFilter(accessToken,file)){
                    String imageUrl = FileUtil.ImageUpload2(file);
                    File newFile = new File(imageUrl);
                    if (!newFile.isFile()) {
                        return R.error(1,imageUrl);
                    }
//                    String imageHtmlUrl = imageUrl.replaceAll("/home/image","https://www.qhspj.com/").replaceAll("/home/file","https://www.qhspj.com/");
                    String imageHtmlUrl = imageUrl.replaceAll("/home/image","https://www.qhspj.com/").replaceAll("/home/file","https://www.qhspj.com/");
                    Map<String,Object> urlMap = new HashMap<>();
                    urlMap.put("imageUrl",imageUrl);
                    urlMap.put("imageHtmlUrl",imageHtmlUrl);
                    urlList.add(imageHtmlUrl);
                    listMap.add(urlMap);
                    //return R.ok().put("url", imageUrl).put("HtmlUrl",imageHtmlUrl);
                }else{
                    return R.error(1,"文件内容涉及敏感内容，请重新上传。");
                }
            }else{
                return R.error(1,"文件为空");
            }
        }
        certificationEntity.setFrontUrl(urlList.get(0));
        certificationEntity.setAfterUrl(urlList.get(1));
        certificationService.save(certificationEntity);
        return R.ok().put("listMap",listMap);
    }
}
